﻿/*///////////////////////////////////////////////////////////
TodoItem.as
Ben Smith
2009
///////////////////////////////////////////////////////////*/

package com.bsmith.app.model 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	/*===========================================================
	TodoItem
	Represents a todo item
	===========================================================*/
	public class TodoItem extends EventDispatcher
	{
		static public const EVENT_CHANGED:String = "ti.chg";
		
		private var _id:uint = 0;					//Todo id -- helps with creation sort
		private var _entry:String = "";				//Entry text
		private var _bCompleted:Boolean = false;	//Is entry completed?
		
		/*-----------------------------------------------------------
		TodoItem - ctor
		-----------------------------------------------------------*/
		public function TodoItem(entry:String="") 
		{
			_entry = entry;
			_bCompleted = false;
			_id = TodoAppState.instance.getNextTodoId();
		}
		
		/*-----------------------------------------------------------
		fromXML
		Creates a new todo item from XML
		-----------------------------------------------------------*/
		static public function fromXML(xml:XML):TodoItem
		{
			var item:TodoItem = new TodoItem;
			item.entry = xml.@entry;
			item.completed = Boolean(int(xml.@completed));
			item.id = xml.@id;
			return item;
		}
		
		/*-----------------------------------------------------------
		serialize
		Creates XML from item
		-----------------------------------------------------------*/
		public function serialize():XML
		{
			var xml:XML = new XML("<Item/>");
			xml.@entry = this.entry;
			xml.@completed = int(this.completed);
			xml.@id = this.id;
			return xml;
		}
		
		/*###########################################################
		GETTERS AND SETTERS
		###########################################################*/
		public function get id():uint { return _id; }
		public function set id(value:uint):void { _id = value; }
		public function get entry():String { return _entry; }
		public function set entry(value:String):void 
		{
			_entry = value;
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
		public function get completed():Boolean { return _bCompleted; }
		public function set completed(value:Boolean):void 
		{
			_bCompleted = value;
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
	}
}